home *** CD-ROM | disk | FTP | other *** search
/ Die Speccy' 97 / Die Speccy' 97.iso / amiga_system / the_aminet / comm / bbs / wwbbs31_source.lha / WWBBS / Lib / wwbbs_lib.c < prev   
C/C++ Source or Header  |  1995-06-20  |  6KB  |  330 lines

  1. #include <exec/types.h>
  2. #include <exec/exec.h>
  3. #include <libraries/wwbbs.h>
  4. #include <ctype.h>
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8.  
  9. #include <proto/exec.h>
  10. #include <proto/dos.h>
  11. #include <proto/wwbbs.h>
  12.  
  13. BOOL Ask(BYTE *prompt,BOOL def)
  14.     {
  15.         BOOL ret=FALSE,kg=TRUE;
  16.         if(prompt)
  17.             printf("~p\n%s (%s,%s)? ",prompt,(def) ? "[Y]" : "Y",(def) ? "N" : "[N]");
  18.         {
  19.             int c;
  20.             while(kg)
  21.                 {
  22.                     c=getchar();
  23.                     if(c==EOF)
  24.                         break;
  25.                     switch(c)
  26.                         {
  27.                             case '\r':
  28.                                 ret=def;
  29.                                 kg=FALSE;
  30.                                 break;
  31.                             case 'Y':
  32.                             case 'y':
  33.                                 ret=TRUE;
  34.                                 kg=FALSE;
  35.                                 break;
  36.                             case 'N':
  37.                             case 'n':
  38.                                 ret=FALSE;
  39.                                 kg=FALSE;
  40.                                 break;
  41.                         }
  42.                 }
  43.         }
  44.         if(!kg)
  45.             {
  46.                 if(ret)
  47.                     printf("~iYes\n");
  48.                 else
  49.                     printf("~iNo\n");
  50.             }
  51.         return(ret);
  52.     }
  53.  
  54. BOOL GetLine(BYTE *buff,UWORD length,ULONG flags)
  55.     {
  56.         BOOL ret=FALSE;
  57.         UWORD index=0;
  58.         printf("~i");
  59.         if(flags & GLFLG_Edit)
  60.             {
  61.                 index=strlen(buff);
  62.                 buff[index]=NULL;
  63.                 printf(buff);
  64.             }
  65.         else
  66.             strcpy(buff,"");
  67.         {
  68.             BOOL kg=TRUE;
  69.             int c;
  70.             while(kg)
  71.                 {
  72.                     c=getchar();
  73.                     if(c==EOF)
  74.                         break;
  75.                     switch(c)
  76.                         {
  77.                             case '\b':
  78.                                 if(index)
  79.                                     {
  80.                                         index--;
  81.                                         buff[index]=NULL;
  82.                                         printf("\b \b");
  83.                                     }
  84.                                 break;
  85.                             case '\r':
  86.                                 if( !( !index && (flags & GLFLG_NoEmpty) ) )
  87.                                     {
  88.                                         printf("\n");
  89.                                         if(index)
  90.                                             ret=TRUE;
  91.                                         kg=FALSE;
  92.                                     }
  93.                                 break;
  94.                             default:
  95.                                 if(isprint(c) && index<length)
  96.                                     {
  97.                                         if(flags & GLFLG_Format)
  98.                                             {
  99.                                                 if(isalpha(c))
  100.                                                     {
  101.                                                         if( (index && !isalpha(buff[index-1])) || !index)
  102.                                                             c=toupper(c);
  103.                                                         else
  104.                                                             c=tolower(c);
  105.                                                     }
  106.                                             }
  107.                                         if(flags & GLFLG_ToLower)
  108.                                             {
  109.                                                 if(isalpha(c))
  110.                                                     c=tolower(c);
  111.                                             }
  112.                                         if(flags & GLFLG_ToUpper)
  113.                                             {
  114.                                                 if(isalpha(c))
  115.                                                     c=toupper(c);
  116.                                             }
  117.                                         if(flags & GLFLG_Chars)
  118.                                             {
  119.                                                 if(!isalpha(c))
  120.                                                     c=NULL;
  121.                                             }
  122.                                         if(flags & GLFLG_Digits)
  123.                                             {
  124.                                                 if(!isdigit(c))
  125.                                                     c=NULL;
  126.                                             }
  127.                                         if(flags & GLFLG_NoChars)
  128.                                             {
  129.                                                 if(isalpha(c))
  130.                                                     c=NULL;
  131.                                             }
  132.                                         if(flags & GLFLG_NoDigits)
  133.                                             {
  134.                                                 if(isdigit(c))
  135.                                                     c=NULL;
  136.                                             }
  137.                                         if(flags & GLFLG_NoSpaces)
  138.                                             {
  139.                                                 if(isspace(c))
  140.                                                     c=NULL;
  141.                                             }
  142.                                         if(flags & GLFLG_NoPunct)
  143.                                             {
  144.                                                 if(ispunct(c))
  145.                                                     c=NULL;
  146.                                             }
  147.                                         if(flags & GLFLG_BeginChar)
  148.                                             {
  149.                                                 if(!index && !isalpha(c))
  150.                                                     c=NULL;
  151.                                             }
  152.                                         if(flags & GLFLG_BeginDigit)
  153.                                             {
  154.                                                 if(!index && !isdigit(c))
  155.                                                     c=NULL;
  156.                                             }
  157.                                         if(c)
  158.                                             {
  159.                                                 sprintf(&buff[strlen(buff)],"%c",c);
  160.                                                 index++;
  161.                                                 if(flags & GLFLG_NoEcho)
  162.                                                     printf(".");
  163.                                                 else
  164.                                                     printf("%c",c);
  165.                                             }
  166.                                     }
  167.                                 break;
  168.                         }
  169.                 }
  170.         }
  171.         return(ret);
  172.     }
  173.  
  174. void ShowText(BYTE *file)
  175.     {
  176.         BYTE rows[4];
  177.         strcpy(rows,"");
  178.         if(GetVar("ROWS",rows,3,NULL)!=-1)
  179.             {
  180.                 FILE *fp=NULL;
  181.                 BYTE file_node[256];
  182.                 int count=0;
  183.                 printf("~o");
  184.                 {
  185.                     BYTE node[33];
  186.                     strcpy(node,"");
  187.                     if(GetVar("NODE",node,32,NULL)!=-1)
  188.                         {
  189.                             sprintf(file_node,"%s.%s",file,node);
  190.                             fp=fopen(file_node,"r");
  191.                         }
  192.                 }
  193.                 if(fp || (fp=fopen(file,"r")))
  194.                     {
  195.                         BYTE buff[256];
  196.                         BOOL ansi_file_check=FALSE,is_ansi_file=FALSE;
  197.                         while(fgets(buff,255,fp))
  198.                             {
  199.                                 if(!ansi_file_check)
  200.                                     {
  201.                                         if(!stricmp(buff,"@ANSI"))
  202.                                             {
  203.                                                 is_ansi_file=TRUE;
  204.                                                 break;
  205.                                             }
  206.                                         ansi_file_check=TRUE;
  207.                                     }
  208.                                 if(WaitForChar(Input(),0))
  209.                                     {
  210.                                         getchar();
  211.                                         break;
  212.                                     }
  213.                                 if(count==atoi(rows)-1)
  214.                                     {
  215.                                         printf("~pMore ([Y],N)? ");
  216.                                         if(!Ask(NULL,TRUE))
  217.                                             break;
  218.                                         printf("~o");
  219.                                         count=0;
  220.                                     }
  221.                                 printf(buff);
  222.                                 count++;
  223.                             }
  224.                         if(is_ansi_file)
  225.                             {
  226.                                 BYTE buff[64];
  227.                                 int num;
  228.                                 while(num=fread(buff,sizeof(BYTE),63,fp))
  229.                                     {
  230.                                         if(WaitForChar(Input(),0))
  231.                                             {
  232.                                                 getchar();
  233.                                                 break;
  234.                                             }
  235.                                         printf("%.*s",num,buff);
  236.                                     }
  237.                             }
  238.                         fclose(fp);
  239.                     }
  240.             }
  241.     }
  242.  
  243. BOOL RunEditor(BYTE *filename)
  244.     {
  245.         BOOL ret=FALSE;
  246.         BYTE editor[256];
  247.         strcpy(editor,"");
  248.         if(GetVar("EDITOR",editor,255,NULL)!=-1)
  249.             {
  250.                 if(strlen(editor))
  251.                     {
  252.                         BYTE cmd[256];
  253.                         BOOL insert_filename=FALSE;
  254.                         {
  255.                             char *p;
  256.                             if(p=strchr(editor,'%'))
  257.                                 {
  258.                                     p++;
  259.                                     if(*p=='f')
  260.                                         {
  261.                                             insert_filename=TRUE;
  262.                                             *p='s';
  263.                                         }
  264.                                 }
  265.                         }
  266.                         if(insert_filename)
  267.                             sprintf(cmd,editor,filename);
  268.                         else
  269.                             sprintf(cmd,"%s \"%s\"",editor,filename);
  270.                         printf("~r");
  271.                         SetMode(Input(),0);
  272.                         SystemTags(cmd,TAG_END);
  273.                         SetMode(Input(),1);
  274.                         {
  275.                             FILE *fp;
  276.                             if(fp=fopen(filename,"r"))
  277.                                 {
  278.                                     fclose(fp);
  279.                                     if(Ask("Save message",TRUE))
  280.                                         ret=TRUE;
  281.                                     else
  282.                                         DeleteFile(filename);
  283.                                 }
  284.                         }
  285.                     }
  286.                 else
  287.                     printf("~s\nPlease select an editor first.\n");
  288.             }
  289.         return(ret);
  290.     }
  291.  
  292. BOOL Pager(BYTE *text,UWORD lines)
  293.     {
  294.         BOOL ret=FALSE;
  295.         BYTE rows[4];
  296.         strcpy(rows,"");
  297.         if(GetVar("ROWS",rows,3,NULL)!=-1)
  298.             {
  299.                 char *p,*q;
  300.                 int count=0;
  301.                 p=text;
  302.                 count=lines;
  303.                 printf("~o");
  304.                 while(q=strchr(p,'\n'))
  305.                     {
  306.                         if(WaitForChar(Input(),0))
  307.                             {
  308.                                 int c;
  309.                                 c=getchar();
  310.                                 if(c=='\r')
  311.                                     ret=TRUE;
  312.                                 break;
  313.                             }
  314.                         if(count==atoi(rows)-1)
  315.                             {
  316.                                 printf("~pMore ([Y],N)? ");
  317.                                 if(!Ask(NULL,TRUE))
  318.                                     break;
  319.                                 printf("~o");
  320.                                 count=0;
  321.                             }
  322.                         q++;
  323.                         printf("%.*s",q-p,p);
  324.                         p=q;
  325.                         count++;
  326.                     }
  327.             }
  328.         return(ret);
  329.     }
  330.